﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace AnLe.Models
{
    public class ProjectsModels
    {
        public static List<Project> getTopProject(int numRecords)
        {
            try
            {
                anleDataContext al = new anleDataContext();
                 return (from m in al.GetTable<Project>()                            
                            select m).Take(numRecords).ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static Project getProjectByID(int proID)
        {
            try
            {
                anleDataContext al = new anleDataContext();
                return (from m in al.GetTable<Project>()
                        where m.ProID == proID
                        select m).First<Project>();
            }
            catch (Exception)
            {
                return null;
            }
        }
        public static bool InsertPro(Project pro)
        {
            try
            {
                anleDataContext data = new anleDataContext();
                data.Projects.InsertOnSubmit(pro);
                data.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                throw;
                return false;
            }
        }
        public static bool UpdatePro(Project pro)
        {
            try
            {
                anleDataContext bs = new anleDataContext();
                var q =
                    (from m in bs.GetTable<Project>()
                     where m.ProID == pro.ProID
                     select m).SingleOrDefault<Project>();
                if (q != null)
                {
                    q.ProID = pro.ProID;
                    q.ProName = pro.ProName;
                    q.ProImg = pro.ProImg;
                    q.ParterName = pro.ParterName;
                    q.Introduction = pro.Introduction;
                    q.Link = pro.Link;                  
                    q.FinishedTime = pro.FinishedTime;
                    q.CateID = pro.CateID;
                    q.Author = pro.Author;
                    q.Discount = pro.Discount;
                 }
                bs.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}